package com.hzz.hzzgateway.base;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 调用返回对象
 *
 * @author lintengyue
 */
@Data
@Accessors(chain = true)
public class ResponseResult<T> implements Serializable {

    //请求结果是否成功
    private String errorCode = CommonResultCode.SUCCESS.getCode();

    //请求返回信息
    private String message = ActionConstants.DEFAULT_SUCCESS_RETURN_MSG;



    private boolean success;

    //请求结果
    private T data = null;

    private ResponseResult() {
    }

    /**
     * 获取正确结果模板
     *
     * @return ResponseResult
     */
    public static ResponseResult<Object> getOK() {
        return getOK(null);
    }

    /**
     * 获取正确结果模板
     *
     * @param obj 请求结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> getOK(T obj) {
        return getOK(ActionConstants.DEFAULT_SUCCESS_RETURN_MSG, obj);
    }

    /**
     * 获取正确结果模板
     *
     * @param message 请求返回信息
     * @param obj     请求结果
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> getOK(String message, T obj) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setMessage(message);
        result.setData(obj);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    /**
     * 获取错误结果模板
     *
     * @return ResponseResult
     */
    public static ResponseResult<Object> getError(ResultCode resultCode) {
        return getError(resultCode, resultCode.getMsg(), null);
    }

    public static ResponseResult<Object> getError(ResultCode commonResultCode, String msg) {
        return getError(commonResultCode, msg, null);
    }

    public static ResponseResult<Object> getError(String errorCode, String msg) {
        ResponseResult<Object> result = new ResponseResult<>();
        result.setErrorCode(errorCode);
        result.setMessage(msg);
        result.setData(null);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static <T> ResponseResult<T> getError(ResultCode resultCode, String message, T obj) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setErrorCode(resultCode.getCode());
        result.setMessage(message);
        result.setData(obj);
        result.setSuccess(Boolean.FALSE);
        return result;
    }


}
